<?php

namespace App\Models\Educaton;

use App\Models\FormInstance;
use App\Models\System\Term;
use App\Models\System\User;
use Illuminate\Database\Eloquent\Model;

class MasterTeacher extends Model
{
    protected $table = 'master_teacher';
    protected $guarded = [];
    protected $hidden = ['updated_at'];
    protected $appends = [
        'grade_set',
        'class_set',
        'chief_set'
    ];

    public function term()
    {
        return $this->hasOne(Term::class, 'id', 'data_version_id');
    } 

    public function creator()
    {
        return $this->hasOne(User::class, 'id', 'creator_id');
    } 

    public function instance()
    {
        return $this->hasOne(FormInstance::class, 'id', 'form_instance_id');
    } 
    
    public function getGradeSetAttribute(){
        $grade = !empty($this->attributes['grade_value']) ? $this->attributes['grade_value'] : '';

        if(!$grade){
            return [];
        }

        return json_decode($grade, true);
    }
    
    public function getClassSetAttribute(){
        $class = !empty($this->attributes['class_value']) ? $this->attributes['class_value'] : '';

        if(!$class){
            return [];
        }

        return json_decode($class, true);
    }
    
    public function getChiefSetAttribute(){
        $chief = !empty($this->attributes['chief_value']) ? $this->attributes['chief_value'] : '';

        if(!$chief){
            return [];
        }

        return json_decode($chief, true);
    }
}
